package mianshi;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;


/**
 *
 * 用BlockingQueue实现生产者消费者
 * Created by JiaJia on 2019/8/13 0013.
 */
public class ProducerConsumer {
    private static ArrayBlockingQueue<String> queue=new ArrayBlockingQueue<>(5);

    private static class Producer extends Thread{
        @Override
        public void run() {
            try {
                queue.put("product");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("produce...");
        }
    }

    private static class Consumer extends Thread{

        @Override
        public void run() {
            try {
                String product=queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("comsume...");
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i <2; i++) {
            Producer producer=new Producer();
            producer.start();
        }
        for (int i = 0; i <5 ; i++) {
            Consumer consumer=new Consumer();
            consumer.start();
        }
        for (int i = 0; i <3 ; i++) {
            Producer producer=new Producer();
            producer.start();
        }
    }



}